package com.pinyoumei.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pinyoumei.app.R;
import com.pinyoumei.app.adapter.NewUserAdapter;
import com.pinyoumei.app.bean.NewUserHelperBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2021-08-27.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-08-27        Administrator             新增Person类
 * 2021-08-27        Administrator             增加sex属性
 * 新手指南
 */
public class NewUserHelperActivity extends BaseActivity implements View.OnClickListener {

    private int page = 1;
    private int market_type = -1;
    private  String title = "";
    private NewUserAdapter newUserAdapter;
    private RecyclerView rv1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_user);
        InitWidget();

        InitData();
    }



    private void InitWidget() {
        RelativeLayout relative_finish = findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(this);
        ImageView iv_1 = findViewById(R.id.iv_1);
        iv_1.setOnClickListener(this);
        ImageView iv_2 = findViewById(R.id.iv_2);
        iv_2.setOnClickListener(this);
        ImageView iv_3 = findViewById(R.id.iv_3);
        iv_3.setOnClickListener(this);
        EditText et_search = findViewById(R.id.et_search);
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索的时候隐藏软键盘
                    hideKeyboard(et_search);
                    // 在这里写搜索的操作,一般都是网络请求数据
                    page = 1;
                    title = et_search.getText().toString().trim();
                    InitData();
                    return true;
                }

                return false;
            }
        });

        rv1 = findViewById(R.id.rv_1);
        rv1.setLayoutManager(new GridLayoutManager(this, 1, GridLayoutManager.VERTICAL, false));
        newUserAdapter = new NewUserAdapter(R.layout.item_new_user, null);
        newUserAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rv1.setAdapter(newUserAdapter);
        newUserAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                Intent intent = new Intent(NewUserHelperActivity.this, WebViewActivity.class);
                intent.putExtra("order_title", newUserAdapter.getData().get(position).getTitle());
                intent.putExtra("url",Constants.MAIN_URL + "h5/userCenter/noviceCourse/detail?market_id=" + newUserAdapter.getData().get(position).getId() );
                startActivity(intent);
            }
        });

        SmartRefreshLayout swipeRefreshLayout  = findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                LoadData();
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                InitData();
                refreshLayout.finishRefresh();
            }
        });
    }


    /**
     * 隐藏软键盘
     * @paramcontext :上下文
     * @param view    :一般为EditText
     */
    public void hideKeyboard(View view) {
        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }



    private void InitData() {
//        全部-1 1平台介绍 2 新人必看 3 账户安全
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(NewUserHelperActivity.this));
        map.put("title", title);
        map.put("market_type", market_type);
        map.put("page", page);
        map.put("limit", 10);


        OkGo.<String>post(Constants.NOVICE_COURSE)
                .params("user_id", SPUtils.getAppUserId(NewUserHelperActivity.this))
                .params("title", title)
                .params("market_type", market_type)
                .params("page", page)
                .params("limit", 10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        NewUserHelperBean newUserHelperBean = gson.fromJson(response.body(), NewUserHelperBean.class);
                        if (newUserHelperBean.getCode() >= 0) {
                            newUserAdapter.setNewData(newUserHelperBean.getResult().getInfo_list());
                            View notDataView2 = getLayoutInflater().inflate(R.layout.item_empty_view, (ViewGroup) rv1.getParent(), false);
                            newUserAdapter.setEmptyView(notDataView2);
                        } else {
                            ToastUtils.showShort(newUserHelperBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }





    private void LoadData() {
//        全部-1 1平台介绍 2 新人必看 3 账户安全
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(NewUserHelperActivity.this));
        map.put("title", title);
        map.put("market_type", market_type);
        map.put("page", page);
        map.put("limit", 10);


        OkGo.<String>post(Constants.NOVICE_COURSE)
                .params("user_id", SPUtils.getAppUserId(NewUserHelperActivity.this))
                .params("title", title)
                .params("market_type", market_type)
                .params("page", page)
                .params("limit", 10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        NewUserHelperBean newUserHelperBean = gson.fromJson(response.body(), NewUserHelperBean.class);
                        if (newUserHelperBean.getCode() >= 0) {
                            newUserAdapter.addData(newUserHelperBean.getResult().getInfo_list());

                        } else {
                            ToastUtils.showShort(newUserHelperBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        	case R.id.relative_finish:
        		finish();
        		break;

        		case R.id.iv_1:
                    page = 1;
                    market_type= 1;
                    InitData();
        		break;

            case R.id.iv_2:
                page = 1;
                market_type= 2;
                InitData();
                break;
            case R.id.iv_3:
                page = 1;
                market_type= 3;
                InitData();
                break;

        	default:
        		break;
        }
    }
}
